// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import LayoutView from '@/views/layout/index.vue'
import LoginView from '@/views/login/index.vue'
import CategoryView from '@/views/Category/index.vue'
import UserView from '@/views/user/index.vue'
import SupplierView from '@/views/Supplier/index.vue'
import WarehouseView from '@/views/warehouse/hshouw.vue'
import WarehousingView from '@/views/warehousing/waresing.vue'
import ConsignorView from '@/views/consignor/index.vue'
import GoodsView from '@/views/Goods/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: LayoutView,
      meta: { requiresAuth: true }, // 添加需要认证的元信息
      children: [
        {path:'/user',name:'user',component:UserView, meta: { requiresAuth: true }},
        {path:'/Category',name:'Category',component:CategoryView, meta: { requiresAuth: true }},
        {path:'/supplier',name:'supplier',component:SupplierView},
        {path:'/warehouse',name:'warehouse',component:WarehouseView},
        {path:'/warehousing',name:'warehousing',component:WarehousingView},
        {path:'/consignor',name:'consignor',component:ConsignorView},
        {path:'/goods',name:'goods',component:GoodsView}
        
      ]
    },
    {path: '/login',name: 'login',component:LoginView}
  ]
})

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.meta.requiresAuth) {
    // 检查localStorage中是否有登录用户信息
    const loginUser = localStorage.getItem('loginUser')
    if (loginUser) {
      // 已登录，继续访问
      next()
    } else {
      // 未登录，重定向到登录页面
      next('/login')
    }
  } else {
    // 不需要认证的路由，直接访问
    next()
  }
})

export default router